/** 【排序初始化示范：】 
var CSORT;
function initSort() {
	var func = function(){
		mggjlist(1,10,null,null);
	};
	CSORT = new CSort();
	CSORT.setPath(path);
	CSORT.setSortFunc(func);
	CSORT.add("startings", "desc", "unlock","t","sort_colum_startings");
	CSORT.add("destination", "desc", "unlock","t","sort_colum_destination");
	CSORT.add("gjlx", "desc", "unlock","t","sort_colum_gjlx");
	CSORT.add("transno", "desc", "unlock","t","sort_colum_transno");
	CSORT.add("countnum", "desc", "unlock","t","sort_colum_countnum");
	CSORT.add("cxrq", "desc", "unlock","t","sort_colum_cxrq");
}*/
/**标题样式示范：
"			<th id='sort_colum_destination' nowrap='nowrap' width='10%' style='cursor:pointer;' >目的地</th> "+
"			<th id='sort_colum_gjlx' nowrap='nowrap' width='10%' style='cursor:pointer;' >出行方式</th> "+
"			<th id='sort_colum_transno' nowrap='nowrap' width='10%' style='cursor:pointer;'>车次/航班</th> "+
"			<th id='sort_colum_countnum' nowrap='nowrap' width='10%' style='cursor:pointer;' >同行总人数</th> "+
"			<th id='sort_colum_cxrq' nowrap='nowrap' width='10%' style='cursor:pointer;' >出行日期</th> "+
**/
/**
 * 作者：张宝宝
 * 时间：2016-09-02
 * 意图：系统中涉及数据查询页面较多，且大多需要进行个别字段的排序分析，因此编写此页代码，方便使用；代码使用稍微还有点耦合，如有能人可稍作改进，谢谢
 * 功能：数据列排序
 * 排序原则：
 * 1.在没有任何一列有排序锁的情况下，点击某列时，以当前列为排序首要，剩余列按先后顺序排序
 * 2.在某一列已有排序锁的情况下，点击剩余某一列时，以拥有排序锁的列为首要，当前列为次要，剩余按先后顺序
 * 3.在有多列拥有排序锁的情况下，排序锁列之间以操作的先后循序进行排序；
 * 总结：排序锁>普通列，操作越靠后，优先级别越高
 * 使用说明：
 * 1.在jsp中导入本js文件；
 * 2.在jsp对应的数据查询js中，加入初始化js代码，如上【排序初始化示范：】 ，并在数据加载之前调用初始化方法，使用默认排序
 * 	 在数据标题的各列标题上添加唯一id,并在标题加载加载完毕时，调用CSORT.addMenu();进行按钮的添加
 * 
 * 关键字：
 * unlock、lock、desc、asc
 * @returns {CSort}
 */
function CSort() {
	var order = 0;
	var sortDataList = {};
	var sortFunc;
	var path;
	var fields = [];
	var dataList=[];
	//设置项目路径
	this.setPath = function(ctx) {
		path = ctx;
	};
	//设置排序时的回调函数
	this.setSortFunc = function(func) {
		sortFunc = func;
	};
	//设置排序所有的字段
	this.add = function(field, sortType, lockStatus,tablename,colum_id) {
		fields.push(field);
		var n_sort= new Sort(field, sortType, lockStatus, sortFunc,tablename,colum_id);
		sortDataList[field] =n_sort;
		dataList.push(n_sort);
	};
	//设置排序单击或双击事件
	this.sortList = function(field, num) {
		var sort = sortDataList[field];
		if(num == 0) {
			sortDataList[field].click();
		} else {
			sortDataList[field].dbclick();
		}
	};
	//封装排序结果参数1（）
	this.getSortResult = function () {
		var array = [];
		var resultStr = "";
		var lockStr="";
		var unlockStr="";
		for(var i=0; i<fields.length; i++) {
			array[i] = sortDataList[fields[i]];
		}
		
		for(var i=0; i<array.length; i++) {
			var len = array.length-i-1;
			for(var k=0; k<len; k++) {
				if(array[k].order >= array[k+1].order) {
					var temp = array[k];
					array[k] = array[k+1];
					array[k+1] = temp;
				}
			}
			//resultStr += array[len].field + "," + array[len].sortType + "," + array[len].lockStatus + "|";
			var tablename = array[len].tablename;
			if(tablename){
				tablename += ".";
			}
			var field = array[len].field;
			if("lock"==array[len].lockStatus){
				if("asc"==array[len].sortType){
					lockStr+=tablename+field+" asc,";
				}else if("desc"==array[len].sortType){
					lockStr+=tablename+field+" desc,";
				}
			}else{
				if("asc"==array[len].sortType){
					unlockStr+=tablename+field+" asc,";
				}else if("desc"==array[len].sortType){
					unlockStr+=tablename+field+" desc,";
				}
			}
		}
		resultStr+=lockStr+unlockStr;
		resultStr=resultStr.substring(0, resultStr.length-1);//去掉最后一个逗号
		return resultStr;
	};
	//设置排序按钮图片
	this.getSortImg = function(field) {
		return sortDataList[field].sortImg;
	};
	//设置排序锁图片
	this.getLockImg = function(field) {
		return sortDataList[field].lockImg;
	};
	//在列标题加载完毕后添加按钮
	this.addMenu=function(){
		if(dataList!=undefined&&dataList.length>0){
			$.each(dataList,function(i,obj){
				var colum_id=obj.colum_id;
				var field=obj.field;
				var sortImg=obj.sortImg;
				var lockImg=obj.lockImg;
				var div="";
				div+="<div style='cursor:pointer;display:inline-block;margin-right:2px;' ondblclick='CSORT.sortList(\""+field+"\", 1)' onclick='CSORT.sortList(\""+field+"\", 0)'>";
				div+=" 	<img id='sort_"+field+"' src='"+sortImg+"'  width='7px' height='13px' style='vertical-align: middle;margin-left:5px'/> <img id='lock_"+field+"' src='"+lockImg+"' width='12px' height='13px' style='vertical-align: middle'/>";
				div+="</div>";
				$("#"+colum_id).append(div);
			});
		}
	};
	//设置排序单击、双击事件对象和逻辑
	var Sort = function(field, sortType, lockStatus, func,tablename,colum_id) {
		var addObj = this;
		addObj.field = field;
		addObj.tablename = tablename;//所属表的别名，区分字段
		addObj.colum_id = colum_id;//页面显示列标题元素id(用于安放按钮图标)
		addObj.sortType = sortType; // 排序方式
		addObj.lockStatus = lockStatus;
		addObj.sortImg = (sortType == "asc" ? path + "/static/js/utils/SORT/up_sort.png" : path + "/static/js/utils/SORT/down_sort.png");
		addObj.lockImg = (lockStatus == "unlock" ? path + "/static/js/utils/SORT/unlock.png" : path + "/static/js/utils/SORT/SORT/lock.png");
		addObj.sortStatus = 0; // 排序状态
		addObj.order = 0;
		
		var timerTask = null;
		addObj.click = function() {
			clearTimeout(addObj.timerTask);
			if(addObj.lockStatus == "lock")
				return;
			addObj.timerTask = setTimeout(function() {
				if(addObj.sortType == "desc") {
					addObj.sortImg = path + "/static/js/utils/SORT/up_sort.png";
					addObj.sortType = "asc";
				} else {
					addObj.sortImg = path + "/static/js/utils/SORT/down_sort.png";
					addObj.sortType = "desc";
				}
				$("#sort_"+field).attr("src", addObj.sortImg);
				addObj.order = ++order;
				func();
			}, 500);
		};
		
		addObj.dbclick = function() {
			clearTimeout(addObj.timerTask);
			if(addObj.lockStatus == "unlock") {
				addObj.lockImg = path + "/static/js/utils/SORT/lock.png";
				addObj.lockStatus = "lock";
			} else {
				addObj.lockImg = path + "/static/js/utils/SORT/unlock.png";
				addObj.lockStatus = "unlock";
			}
			$("#lock_"+field).attr("src", addObj.lockImg);
			addObj.order = ++order;
		};
	};
	
	

}
